<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


class TripController extends Controller
{

    public function banners(){
        $banner = DB::table('banner')->limit(6)->orderBy('id','desc')->get();
        $banner = objectToArray($banner);
        foreach ($banner as $k=>$v){
            $banner[$k]['image'] = config('app.url').'/upload/'.$v['image'];
        }
        return response()->json(['code'=>0,'data'=>$banner]);
    }

    public function category(){
        $lists = DB::table('trip')->where('status',1)->get();
        $lists =  objectToArray($lists);
        $arr = [];
        $n=0;
        for($i=0;$i<count($lists);$i++){
            $arr[$n]=$lists[$i]['start_addr'];
            //$arr[$n+1]=$lists[$i]['end_addr'];
            $n = $n+1;
        }
        $arr_new = [];
        foreach ($arr as $k=>$v){
            if (!empty($v)){
            $count = DB::table('trip')
                ->where('status',1)
                ->where(function($query) use ($v){
                    if (!empty($city)){
                        $query->where('start_addr',$city);
                    }
                })->count();
            $arr_new[$k] = ["count"=>$count,"val"=>$v];
            }
        }
        $last_names = array_column($arr_new,'count');
        array_multisort($last_names,SORT_DESC,$arr_new);
        $city = [];
        if ($arr_new){
            $arr_new = array_values($arr_new);
            $city = array_column($arr_new,'val');
            $city = array_unique($city);
            $city = array_values($city);
        }
        return response()->json(['code'=>0,'data'=>$city]);

    }

    public function search(Request $request)
    {
        $keywords = $request->input('keywords',"");

        $citys = DB::table('trip')
            ->where('status',1)
            ->where(function($query) use ($keywords){
                if (!empty($keywords)){
                    $query->where('start_addr','like',"%$keywords%")->orWhere('end_addr','like',"%$keywords%");
                }
            })->orderBy('id','desc')->paginate(8);
        $citys =  objectToArray($citys);

        $data = [
            'keywords'=>$keywords,
            "trip_list"=>$citys
        ];
        return response()->json(['code'=>0,'data'=>$data]);

    }

    public function lists(Request $request){

        $city = $request->input('city'," ");
        $order = $request->input('order',1);
        if ($order==1){
            $orderBy = "trip_date desc";
        }else if ($order==2){
            $orderBy = "trip_date asc";
        }else if ($order==3){
            $orderBy = "trip_price desc";
        }else{
            $orderBy = "trip_date desc";
        }

        $citys = DB::table('trip')
            ->where('status',1)
            ->where(function($query) use ($city){
                if (!empty($city)){
                    $query->where('start_addr',$city)->orWhere('end_addr',$city);
                }
            })->orderByRaw(DB::raw($orderBy))->paginate(8);
        $citys =  objectToArray($citys);

        $data = [
            'cur_city'=>$city,
            "trip_list"=>$citys
        ];
        return response()->json(['code'=>0,'data'=>$data]);

    }

    public function detail(Request $request){
        $id = $request->input('id',"");
        $user_id = $request->input('user_id',"");
        $citys = DB::table('trip')->where('status',1)->find($id);
        if (empty($citys)){
            return response()->json(['code'=>-10001,'msg'=>'详情不存在']);
        }
        if (empty($user_id)){
            return response()->json(['code'=>-10001,'msg'=>'用户id不能为空']);
        }
        DB::table("trip_read_log")->insertGetId([
            'trip_id'=>$id,
            'user_id'=>$user_id,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ]);
        return response()->json(['code'=>0,'data'=>$citys]);

    }

    public function add(Request $request){
        

        $user_id = $request->input('user_id',0);
        $start_province = $request->input('start_province');
        $start_city = $request->input('start_city');
        $start_area = $request->input('start_area');
        $end_province = $request->input('end_province');
        $end_city = $request->input('end_city');
        $end_area = $request->input('end_area');
        $start_addr = $request->input('start_addr');
        $end_addr = $request->input('end_addr');
        $select_type = $request->input('select_type');
        $trip_date = $request->input('trip_date');
        $trip_price = $request->input('trip_price');
        $trip_num = $request->input('trip_num');
        $is_charter = $request->input('is_charter',0);
        $contacter = $request->input('contacter');
        $moblie = $request->input('moblie');
        $car_code = $request->input('car_code');
        $remark = $request->input('remark');

        if (empty($user_id)){return response()->json(['code'=>-10001,'msg'=>'user_id not empty']);}
        if (empty($start_province)){return response()->json(['code'=>-10001,'msg'=>'start_province not empty']);}
        if (empty($start_city)){return response()->json(['code'=>-10001,'msg'=>'$start_city not empty']);}
        if (empty($start_area)){return response()->json(['code'=>-10001,'msg'=>'$start_area not empty']);}
        if (empty($end_province)){return response()->json(['code'=>-10001,'msg'=>'$end_province not empty']);}
        if (empty($end_city)){return response()->json(['code'=>-10001,'msg'=>'$end_city not empty']);}
        if (empty($end_area)){return response()->json(['code'=>-10001,'msg'=>'$end_area not empty']);}
        //if (empty($start_addr)){return response()->json(['code'=>-10001,'msg'=>'start_addr not empty']);}
        //if (empty($end_addr)){return response()->json(['code'=>-10001,'msg'=>'end_addr not empty']);}
        if (empty($select_type)){return response()->json(['code'=>-10001,'msg'=>'select_type not empty']);}
        if (empty($trip_date)){return response()->json(['code'=>-10001,'msg'=>'trip_date not empty']);}
        if (empty($trip_price)  && $select_type==1){return response()->json(['code'=>-10001,'msg'=>'trip_price not empty']);}
        if (empty($trip_num)){return response()->json(['code'=>-10001,'msg'=>'trip_num not empty']);}
        //if (empty($is_charter)){return response()->json(['code'=>-10001,'msg'=>'is_charter not empty']);}
        if (empty($contacter)){return response()->json(['code'=>-10001,'msg'=>'contacter not empty']);}
        if (empty($moblie)){return response()->json(['code'=>-10001,'msg'=>'moblie not empty']);}
        if (empty($car_code) && $select_type==2){return response()->json(['code'=>-10001,'msg'=>'car_code not empty']);}


        $temp = [
            'user_id'=>$user_id,
            'start_province'=>$start_province,
            'start_city'=>$start_city,
            'start_area'=>$start_area,
            'end_province'=>$end_province,
            'end_city'=>$end_city,
            'end_area'=>$end_area,
            'start_addr'=>$start_addr,
            'end_addr'=>$end_addr,
            'select_type'=>$select_type,
            'trip_date'=>$trip_date,
            'trip_price'=>$trip_price,
            'trip_num'=>$trip_num,
            'is_charter'=>$is_charter,
            'contacter'=>$contacter,
            'moblie'=>$moblie,
            'car_code'=>$car_code,
            'remark'=>$remark,
            'status'=>1,
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
        $res = DB::table('trip')->insertGetId($temp);
        $temp['id'] = $res;


        return response()->json(['code'=>0,'data'=>$temp]);

    }

    public function edit(Request $request){


        $trip_id = $request->input('id');
        $user_id = $request->input('user_id',0);
        $start_province = $request->input('start_province');
        $start_city = $request->input('start_city');
        $start_area = $request->input('start_area');
        $end_province = $request->input('end_province');
        $end_city = $request->input('end_city');
        $end_area = $request->input('end_area');
        $start_addr = $request->input('start_addr');
        $end_addr = $request->input('end_addr');
        $select_type = $request->input('select_type');
        $trip_date = $request->input('trip_date');
        $trip_price = $request->input('trip_price');
        $trip_num = $request->input('trip_num');
        $is_charter = $request->input('is_charter');
        $contacter = $request->input('contacter');
        $moblie = $request->input('moblie');
        $car_code = $request->input('car_code');
        $remark = $request->input('remark');

        if (empty($trip_id)){return response()->json(['code'=>-10001,'msg'=>'trip_id not empty']);}
        if (empty($user_id)){return response()->json(['code'=>-10001,'msg'=>'user_id not empty']);}
        if (empty($start_province)){return response()->json(['code'=>-10001,'msg'=>'start_province not empty']);}
        if (empty($start_city)){return response()->json(['code'=>-10001,'msg'=>'$start_city not empty']);}
        if (empty($start_area)){return response()->json(['code'=>-10001,'msg'=>'$start_area not empty']);}
        if (empty($end_province)){return response()->json(['code'=>-10001,'msg'=>'$end_province not empty']);}
        if (empty($end_city)){return response()->json(['code'=>-10001,'msg'=>'$end_city not empty']);}
        if (empty($end_area)){return response()->json(['code'=>-10001,'msg'=>'$end_area not empty']);}
        /*if (empty($start_addr)){return response()->json(['code'=>-10001,'msg'=>'start_addr not empty']);}
        if (empty($end_addr)){return response()->json(['code'=>-10001,'msg'=>'end_addr not empty']);}*/
        if (empty($select_type)){return response()->json(['code'=>-10001,'msg'=>'select_type not empty']);}
        if (empty($trip_date)){return response()->json(['code'=>-10001,'msg'=>'trip_date not empty']);}
        if (empty($trip_price)  && $select_type==1){return response()->json(['code'=>-10001,'msg'=>'trip_price not empty']);}
        if (empty($trip_num)){return response()->json(['code'=>-10001,'msg'=>'trip_num not empty']);}
        if (empty($is_charter)){return response()->json(['code'=>-10001,'msg'=>'is_charter not empty']);}
        if (empty($contacter)){return response()->json(['code'=>-10001,'msg'=>'contacter not empty']);}
        if (empty($moblie)){return response()->json(['code'=>-10001,'msg'=>'moblie not empty']);}
        if (empty($car_code)  && $select_type==2){return response()->json(['code'=>-10001,'msg'=>'car_code not empty']);}

        $s = DB::table('trip')->where('user_id',$user_id)->where('id',$trip_id)->first();
        if (empty($s)){
            return response()->json(['code'=>-10001,'msg'=>'行程不存在']);
        }


        $temp = [
            'user_id'=>$user_id,
            'start_province'=>$start_province,
            'start_city'=>$start_city,
            'start_area'=>$start_area,
            'end_province'=>$end_province,
            'end_city'=>$end_city,
            'end_area'=>$end_area,
            'start_addr'=>$start_addr,
            'end_addr'=>$end_addr,
            'select_type'=>$select_type,
            'trip_date'=>$trip_date,
            'trip_price'=>$trip_price,
            'trip_num'=>$trip_num,
            'is_charter'=>$is_charter,
            'contacter'=>$contacter,
            'moblie'=>$moblie,
            'car_code'=>$car_code,
            'remark'=>$remark,
            'status'=>1,
            'updated_at'=>date('Y-m-d H:i:s'),
        ];
        $res = DB::table('trip')->where('user_id',$user_id)->where('id',$trip_id)->update($temp);
        $temp['id'] = $trip_id;

        return response()->json(['code'=>0,'data'=>$temp]);

    }

    public function del(Request $request){
        $trip_id = $request->input('id');
        $user_id = $request->input('user_id',0);
        $s = DB::table('trip')->where('user_id',$user_id)->where('id',$trip_id)->first();
        if (empty($s)){
            return response()->json(['code'=>-10001,'msg'=>'行程不存在']);
        }
        DB::table('trip')->where('user_id',$user_id)->where('id',$trip_id)->delete();
        return response()->json(['code'=>0,'data'=>['id'=>$trip_id]]);
    }



}